import { z } from "zod"; import { withUser, json, parseBody } from "@/lib/api"; import { getFile, updateFile, deleteFile, toPublicFile } from "@/lib/library/service"; export const dynamic = "force-dynamic"; type P = { id: string }; /** * GET /api/library/files/:id — streams the payload to its owner (image previews, downloads). * `?meta=1` returns the metadata JSON instead; `?download=1` forces an attachment disposition. */ export const GET = withUser
(async ({ req, user }, { id }) => { const p = new URL(req.url).searchParams; const row = await getFile(user.id, id); if (p.get("meta") === "1") return json({ file: toPublicFile(row) }); const buf = Buffer.from(row.dataBase64, "base64"); const disposition = p.get("download") === "1" ? "attachment" : "inline"; return new Response(buf, { headers: { "Content-Type": row.mimeType, "Content-Length": String(buf.length), "Cache-Control": "private, max-age=3600", "Content-Disposition": `${disposition}; filename="${encodeURIComponent(row.name)}"`, "X-Content-Type-Options": "nosniff", }, }); }); const patchSchema = z.object({ name: z.string().min(1).max(200).optional(), description: z.string().max(500).nullable().optional(), /** Move to a project, or `null` for the global library. */ projectId: z.string().max(64).nullable().optional(), }); export const PATCH = withUser
(async ({ req, user }, { id }) => { const body = await parseBody(req, patchSchema); return json({ file: await updateFile(user.id, id, body) }); }); export const DELETE = withUser
(async ({ user }, { id }) => { await deleteFile(user.id, id); return json({ ok: true }); });